//
//  main.swift
//  niukewang
//
//  Created by Mr.Deng on 2024/3/11.
//

import Foundation

print("Hello, World!")

//参数：16进制的数
func transform16To10(num: String) {
    var input = num
    var strs = Array(input).map{ String($0) }.reversed()
    let map: [String: Int] = [
        "A": 10,
        "B": 11,
        "C": 12,
        "D": 13,
        "E": 14,
        "F": 15
        ]
    var ans: Decimal = 0
    for (index, value) in strs.enumerated() {
        var num = map.keys.contains(value) ? map[value] ?? 0 : Int(value) ?? 0
        let res = NSDecimalNumber(decimal: pow(16, index))
        ans += Decimal(num) * pow(16, index)
    }
    print(ans)
}

var str = "123".map{ String($0) }


transform16To10(num: "0xABC")

/*
var array = [String]()
var x = 0, y = 0
while let line = readLine() {
    let str = line.components(separatedBy: ";")
    array = Array(str)
    for item in array {
        //先判断是否是有效输入
        let chars = item.map{ String($0) }
        if(chars.count < 2) { continue }
        if(chars[0] != "A" && chars[0] != "W" && chars[0] != "D" && chars[0] != "S") {
            //第一位不符合条件，过滤
            continue
        }
        let suffix = Array(chars[1...chars.count - 1]).joined(separator: "")
        guard let num = Int(suffix) else { continue }
        if(num > 0 && num < 100) {
            switch chars[0] {
            case "A":
                x -= num
            case "D":
                x += num
            case "W":
                y += num
            case "S":
                y -= num
            default:
                break
            }
        }
    }
    print(String(x) + "," + String(y))
}
*/

var isOk = true
var array = [String]()
var typeArray = [0, 0, 0 ,0]
while let line = readLine() {
    var str = line
    if (str.isEmpty) { break }
    array.append(str)
    checkPwd(str)
}

for item in array {
    print(checkPwd(item))
}

func checkPwd(_ item: String) -> String {
    if (item.count <= 8) {
        return "NG"
    }
    let chars = Array(item).map{ String($0) }
    for item in chars {
        if (item >= "A" && item <= "Z") {
            typeArray[0] = 1
        } else if (item >= "a" && item <= "z") {
            typeArray[1] = 1
        } else if (Int(item) ?? -1 >= 0 && Int(item) ?? 10 <= 9) {
            typeArray[2] = 1
        } else {
            typeArray[3] = 1
        }
    }
    if(typeArray.reduce(0, +)) < 3 {
        return "NG"
    }
    //接下来判断重复子字符串
    for index in 0..<chars.count - 3 {
        let startIndex = String.Index(encodedOffset: index)
        let endIndex = String.Index(encodedOffset: index + 3)
        var subStr = String(item[startIndex...endIndex])
        let lastIndex = item.lastIndex(of: subStr)
        let res = item[0..<lastIndex].count
        if(res != index) {
            return "NG"
        }
    }
    return "OK"
}
